package org.eiahe.hr.attendance.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.core.validate.AddGroup;
import org.ehe.common.core.validate.EditGroup;
import org.ehe.common.excel.utils.ExcelUtil;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.eiahe.hr.attendance.domain.bo.OvertimeRuleBo;
import org.eiahe.hr.attendance.domain.vo.OvertimeRuleVo;
import org.eiahe.hr.attendance.service.IOvertimeRuleService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 加班规则管理控制器
 *
 * @author zhangnn
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/attendance/overtimeRule")
public class OvertimeRuleController extends BaseController {

    private final IOvertimeRuleService overtimeRuleService;

    /**
     * 查询加班规则列表
     */
    @SaCheckPermission("attendance:overtimeRule:list")
    @GetMapping("/list")
    public TableDataInfo<OvertimeRuleVo> list(OvertimeRuleBo bo, PageQuery pageQuery) {
        return overtimeRuleService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取加班规则详细信息
     */
    @SaCheckPermission("attendance:overtimeRule:query")
    @GetMapping("/{ruleId}")
    public R<OvertimeRuleVo> getInfo(@PathVariable Long ruleId) {
        return R.ok(overtimeRuleService.queryById(ruleId));
    }

    /**
     * 新增加班规则
     */
    @SaCheckPermission("attendance:overtimeRule:add")
    @Log(title = "新增加班规则", linkType = "attendance", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody OvertimeRuleBo bo) {
        return toAjax(overtimeRuleService.insertByBo(bo));
    }

    /**
     * 修改加班规则
     */
    @SaCheckPermission("attendance:overtimeRule:edit")
    @Log(title = "修改加班规则", linkType = "attendance", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody OvertimeRuleBo bo) {
        return toAjax(overtimeRuleService.updateByBo(bo));
    }

    /**
     * 删除加班规则
     */
    @SaCheckPermission("attendance:overtimeRule:remove")
    @Log(title = "删除加班规则", linkType = "attendance", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ruleIds}")
    public R<Void> remove(@PathVariable Long[] ruleIds) {
        return toAjax(overtimeRuleService.deleteWithValidByIds(List.of(ruleIds)));
    }

    /**
     * 根据班次ID查询加班规则
     */
    @SaCheckPermission("attendance:overtimeRule:query")
    @GetMapping("/shift/{shiftId}")
    public R<OvertimeRuleVo> getByShiftId(@PathVariable Long shiftId) {
        return R.ok(overtimeRuleService.queryByShiftId(shiftId));
    }

    /**
     * 导出加班规则列表
     */
    @SaCheckPermission("attendance:overtimeRule:export")
    @Log(title = "导出加班规则", linkType = "attendance", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(OvertimeRuleBo bo, HttpServletResponse response) {
        List<OvertimeRuleVo> list = overtimeRuleService.queryList(bo);
        ExcelUtil.exportExcel(list, "加班规则数据", OvertimeRuleVo.class, response);
    }
}
